package com.liberation.aidiancan.utils;

/**
 * 项目名称： AiDianCan
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 10:53
 */

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.annotation.CacheLevel;
import com.okhttplib.callback.CallbackOk;
import com.orhanobut.logger.Logger;

import java.io.IOException;
import java.util.List;

public class NetworkUtils {
	private static String result;

	/**
	 * 网络是否可用
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}


	/**
	 * Gps是否打开
	 *
	 * @param context
	 * @return
	 */
	public static boolean isGpsEnabled(Context context) {
		LocationManager locationManager = ((LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE));
		List<String> accessibleProviders = locationManager.getProviders(true);
		return accessibleProviders != null && accessibleProviders.size() > 0;
	}

	/**
	 * wifi是否打开
	 */
	public static boolean isWifiEnabled(Context context) {
		ConnectivityManager mgrConn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		TelephonyManager mgrTel = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
				.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
				.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
	}

	/**
	 * 判断当前网络是否是wifi网络
	 * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {
	 *
	 * @param context
	 * @return boolean
	 */
	public static boolean isWifi(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}
		return false;
	}

	/**
	 * 判断当前网络是否3G网络
	 *
	 * @param context
	 * @return boolean
	 */
	public static boolean is3G(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
			return true;
		}
		return false;
	}

	/**
	 * 返回当前网络装状态
	 *
	 * @param context
	 * @return boolean
	 */

	public static String getCurrentNetType(Context context) {
		String type = "";
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info == null) {
			type = "null";
		} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
			type = "wifi";
		} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
			int subType = info.getSubtype();
			if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
					|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
				type = "2g";
			} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
				type = "3g";
			} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡，是3.9G的全球标准
				type = "4g";
			}
		}
		Logger.e("msg", "联网状态为：" + type);
		return type;
	}
	/**
	 * 同步请求：由于不能在UI线程中进行网络请求操作，所以采用子线程方式
	 *//*
	private void (final String url, final GetJson getJson, final Context context) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpInfo info = HttpInfo.Builder().setUrl(url).build();
				OkHttpUtil.getDefault(context).doGetSync(info);
				if (info.isSuccessful()) {
					final String result = info.getRetDetail();
					context.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							resultTV.setText("同步请求：" + result);
						}
					});
				}
			}
		}).start();
	}*/

	/**
	 * 异步请求：回调方法可以直接操作UI
	 */
	public static void doHttpAsync(final String url, final GetJson getJson, Context context) {
		result = null;
		OkHttpUtil.Builder().setCacheLevel(CacheLevel.FIRST_LEVEL).setConnectTimeout(25).build(context).doGetAsync(
				HttpInfo.Builder().setUrl(url).build(),
				new CallbackOk() {
					@Override
					public void onResponse(HttpInfo info) throws IOException {
						if (info.isSuccessful()) {
							result = info.getRetDetail();
							getJson.getJsonCallBack(result, url);
						}
					}
				});


	}

	/**
	 * 缓存请求：请连续点击缓存请求，会发现在缓存有效期内，从第一次请求后的每一次请求花费为0秒，说明该次请求为缓存响应
	 */
	public static void doHttpCache(final String url, final GetJson getJson, Context context) {
		result = null;
		OkHttpUtil.Builder()
				.setCacheLevel(CacheLevel.SECOND_LEVEL)
				.build(context)
				.doGetAsync(
						HttpInfo.Builder().setUrl(url).build(),
						new CallbackOk() {
							@Override
							public void onResponse(HttpInfo info) throws IOException {
								if (info.isSuccessful()) {
									result = info.getRetDetail();
									getJson.getJsonCallBack(result, url);
								}
							}
						});
	}

	public interface GetJson {
		void getJsonCallBack(String result, String request_url);

	}
}